#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "ffmpegutils.h"
#include "audioresample.h"
#include <QTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{

//    ResamplePCMThread * thread = new ResamplePCMThread(this);

//    connect(thread,&QThread::finished,[&thread](){

//        thread = nullptr;
//    });

//    thread->start();

    resamplePCMThread = new ResamplePCMThread(this);

    connect(resamplePCMThread,&QThread::finished,[this](){

        qDebug() << "resamplePCMThread finished";
        resamplePCMThread = nullptr;
    });

    resamplePCMThread->start();

//    AudioResample *audioResample = new AudioResample();

//    audioResample->start();
}

void MainWindow::on_recodingWAV_clicked()
{

    if (recordingWAVAudio == nullptr) {

        ui->recodingWAV->setText("暂停");
        recordingWAVAudio = new RecordingWAVAudio(this);

        connect(recordingWAVAudio,&QThread::finished,[this]() {

            ui->recodingWAV->setText("录WAV");
            recordingWAVAudio = nullptr;
        });
        connect(recordingWAVAudio,&RecordingWAVAudio::timeCallBack,this,&MainWindow::showRecordingTime);
        recordingWAVAudio->recordingWAV();

    }else {

        recordingWAVAudio->requestInterruption();
    }




}

void MainWindow::showRecordingTime(unsigned long long time)
{


    QTime qTime(0,0,0,0);
    QString showTime = qTime.addMSecs(time).toString("HH:mm:ss:z");
    qDebug() << "time:" << time << "showTime: "  << showTime;
    ui->recodingTimeLabel->setText(showTime.left(10));
}

















